﻿namespace Rsdn.Janus
{
    using System;
    using SmartApp;

    public static class ImagesHelper
    {
        /// <summary>
        /// Возвращает путь к иконке, ассоциированной с классом пользователя.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="userClass">Класс пользователя.</param>
        /// <returns>Путь к иконке, ассоциированной с классом пользователя.</returns>
        public static string GetUserImagePath(IServiceProvider provider, UserClass userClass)
        {
            string name = userClass != UserClass.User ? userClass.ToString() : String.Empty;

            return GetImageUri(provider, @"MessageTree\User" + name, StyleImageType.ConstSize);
        }

        /// <summary>
        /// Возвращает путь к иконке сообщения.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="isRead">Сообщение прочитано</param>
        /// <param name="isMarked">Помечено ли сообщение флагом (очками).</param>
        /// <param name="isArticle">Является ли сообщение статьей.</param>
        /// <returns>Путь к иконке сообщения.</returns>
        public static string GetMessageImagePath(IServiceProvider provider, bool isRead, bool isMarked, bool isArticle)
        {
            const string Marked = "Marked";
            const string Article = "Article";
            const string Unread = "Unread";

            string path;
            string article = String.Empty;
            string marked = String.Empty;
            string read = "";

            if (isArticle)
            {
                article = Article;
            }

            if (isMarked)
            {
                marked = Marked;
            }

            if (!isRead)
            {
                if (isMarked)
                {
                    read = Unread + "2";
                }
                else
                {
                    read = Unread;
                }
            }

            path = String.Format("Msg{0}{1}{2}", article, read, marked);
            //if (isArticle)
            //{
            //    path = isRead
            //               ? isMarked ? "MsgArticleMarked" : "MsgArticle"
            //               : isMarked ? "MsgArticleUnread2Marked" : "MsgArticleUnread";
            //}
            //else
            //{
            //    path = isRead
            //               ? isMarked ? "MsgMarked" : "Msg"
            //               : isMarked ? "MsgUnread2Marked" : "MsgUnread";
            //}

            return GetImageUri(provider, @"MessageTree\" + path, StyleImageType.ConstSize);
        }

        /// <summary>
        /// Gets the week day image path.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="weekDay">The week day.</param>
        /// <param name="outdated">if set to <c>true</c> [outdated].</param>
        /// <returns></returns>
        public static string GetWeekDayImagePath(IServiceProvider provider, int weekDay, bool outdated)
        {
            string text = String.Format(@"MessageTree\WD{0}{1}", 
                                        outdated ? "OUT" : String.Empty, 
                                        weekDay);

            IStyleImageManager styleImageManager = provider.GetRequiredService<IStyleImageManager>();

            return styleImageManager.GetImageUri(text, StyleImageType.ConstSize);
        }

        /// <summary>
        /// Возвращает путь к иконке с оценкой.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="rate">Оценка.</param>
        /// <returns>Путь к иконке с оценкой.</returns>
        public static string GetRateImagePath(IServiceProvider provider, MessageRates rate)
        {
            switch (rate)
            {
                case MessageRates.Rate1:
                case MessageRates.Rate2:
                case MessageRates.Rate3:
                    return GetImageUri(provider, @"MessageViewer\" + rate, StyleImageType.Small);
            }

            return GetImageUri(provider, @"MessageViewer\Rate" + rate, StyleImageType.Small) ?? String.Empty;
        }

        /// <summary>
        /// Возвращает путь к иконке форума.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="isUnread">В форуме есть не прочитанные сообщения.</param>
        public static string GetForumImagePath(IServiceProvider provider, bool isUnread)
        {
            string path =
                isUnread
                    ? @"NavTree\ForumUnread"
                    : @"NavTree\Forum";

            return GetImageUri(provider, path, StyleImageType.ConstSize);
        }

        /// <summary>
        /// Возвращает путь к иконке, ассоциированной с ресурсом.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="resourceType">Тип ресурса.</param>
        /// <returns>Путь к иконке, которая ассоциирована с ресурсом.</returns>
        public static string GetResourceImagePath(IServiceProvider provider, JanusProtocolResourceType resourceType)
        {
            string name;

            switch (resourceType)
            {
                case JanusProtocolResourceType.FaqList: name = "faqall"; break;
                case JanusProtocolResourceType.ArticleList: name = "articleall"; break;
                case JanusProtocolResourceType.UserInfo: name = "ExtBrowser"; break;
                case JanusProtocolResourceType.UserRating: name = "userratein"; break;
                case JanusProtocolResourceType.UserOutrating: name = "userrateout"; break;
                case JanusProtocolResourceType.UserMessages: name = "ShowAllMsgs"; break;
                case JanusProtocolResourceType.UserMessagesStat: name = "sh_all_msgs"; break;
                case JanusProtocolResourceType.TeamList: name = "admin"; break;
                case JanusProtocolResourceType.MessageRate:
                    return GetImageUri(provider, "RateGroup", StyleImageType.ConstSize);
                default:
                    return GetImageUri(provider, @"ForumImages\webref", StyleImageType.ConstSize);
            }

            return GetImageUri(provider, name, StyleImageType.Small);
        }

        /// <summary>
        /// Получить путь до картинки.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="name">Имя картинки.</param>
        /// <param name="imageType">Тип картинки.</param>
        /// <returns>Путь до картинки.</returns>
        public static string GetImageUri(IServiceProvider provider, string name, StyleImageType imageType)
        {
            IStyleImageManager styleImageManager = provider.GetRequiredService<IStyleImageManager>();

            return styleImageManager.GetImageUri(name, imageType);
        }
    }
}